﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NovelDB.Entity;
using NHibernate.Criterion;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace NovelDB.Helper
{
    public class CriticalQuery
    {
        /// <summary>
        /// 获得所有的小说
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static List<Novel> GetAllNovels(ISession session)
        {
            var res = session.CreateQuery("select novel from Novel as novel").List<Novel>();

            return res as List<Novel>;
        }

        /// <summary>
        /// 根据小说的名字和站点名查找小说
        /// </summary>
        /// <param name="session"></param>
        /// <param name="novelName"></param>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public static Novel GetNovelByNameAndSite(ISession session, string novelName, string siteName)
        {
            Novel res = session.CreateCriteria<Novel>().Add(Restrictions.Eq("Name", novelName))
                .Add(Restrictions.Eq("SiteName", siteName))
                .UniqueResult<Novel>();

            return res;
        }

        /// <summary>
        /// 获得所有需要更新的小说链接
        /// </summary>
        /// <param name="session"></param>
        /// <param name="novel"></param>
        /// <returns></returns>
        public static List<NovelLink> GetNeedUpdateLinks(ISession session, Novel novel)
        {
            var res = session.CreateQuery(@"select link 
                    from NovelChapter as chapter , NovelLink as link
                    where chapter.Novel = :novel
                    and link.NovelChapter = chapter
                    and link.IsUpdated = false").SetInt32("novel", novel.Id).List<NovelLink>();

            return res as List<NovelLink>;
        }

        /// <summary>
        /// 根据小说的ID和章节的ID查找对应的章节
        /// </summary>
        /// <param name="session"></param>
        /// <param name="novelID"></param>
        /// <param name="chapID"></param>
        /// <returns></returns>
        public static NovelChapter GetChapterByNovelIdAndChapterId(ISession session, Novel novel, int chapID)
        {
            NovelChapter res = session.CreateCriteria<NovelChapter>().Add(Restrictions.Eq("ChapterId", chapID))
                .Add(Restrictions.Eq("Novel", novel)).UniqueResult<NovelChapter>();

            return res;
        }

        public static NovelLink GetLinkByLinkName(ISession session, int novelId, int linkName)
        {
            NovelLink res = session.CreateCriteria<NovelLink>()
                .Add(Restrictions.Eq("Name", linkName)).UniqueResult<NovelLink>();

            return res;
        }

        public static NovelLink GetLinkByLinkId(ISession session, int novelId, int linkId)
        {
            NovelLink res = session.CreateCriteria<NovelLink>()
                .Add(Restrictions.Eq("LinkId", linkId)).UniqueResult<NovelLink>();

            return res;
        }
    }
}
